package com.example.day0210;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.day0210.R;
import com.example.day0210.adapter.MyAdapter;
import com.example.day0210.fragment.Fragment1;
import com.example.day0210.fragment.Fragment2;
import com.example.day0210.fragment.StartFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class StartActivity extends AppCompatActivity {
    private TextView time;
    private Button btn;
    private int count = 5;


    private Timer timer;
    private ViewPager vp;
    private LinearLayout ll;
    private List<Fragment> list = new ArrayList<>();
    private ArrayList<ImageView> imageViews;
    private StartFragment startFragment;
    private int index = 0;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 100) {
                vp.setCurrentItem(index);
                index++;
                //停止自动翻页
                if (index == list.size()) {
                    timer.cancel();
                }
            }
            if (msg.what == 101) {

            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        vp = (ViewPager) findViewById(R.id.vp);
        ll = (LinearLayout) findViewById(R.id.ll);
        time = (TextView) findViewById(R.id.time);
        btn = (Button) findViewById(R.id.btn);


        list = new ArrayList<>();
        list.add(new Fragment1());
        list.add(new Fragment2());
        list.add(new StartFragment());

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(StartActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

        imageViews = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(R.drawable.cricle);
            if (i == 0) {
                imageView.setImageResource(R.drawable.cricles);
            }
            imageViews.add(imageView);
            ll.addView(imageView);
        }


        vp.setAdapter(new MyAdapter(getSupportFragmentManager(), list));

        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(100);
            }
        }, 0, 1500);

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == list.size() - 1) {
                    time.setVisibility(View.VISIBLE);
                    btn.setVisibility(View.VISIBLE);

                    Timer t = new Timer();
                    t.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    count--;
                                    time.setText("倒计时" + count + "秒");
                                    if (count == 0) {
                                        timer.cancel();
                                        Intent intent = new Intent(StartActivity.this, MainActivity.class);
                                        startActivity(intent);
                                        finish();
                                    }
                                }
                            });
                        }
                    }, 0, 1000);




                }
                for (int i = 0; i < imageViews.size(); i++) {
                    if (i == position) {
                        imageViews.get(i).setImageResource(R.drawable.cricles);
                    } else {
                        imageViews.get(i).setImageResource(R.drawable.cricle);
                    }
                }


            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

    }
}
